package com.kingstar.ceres.data;

import com.kingstar.ceres.data.db.OriginalCsvChannelImpl;
import com.kingstar.ceres.data.handler.AbstractDataHandler;
import com.kingstar.ceres.data.handler.ITaskLockHandler;
import com.kingstar.ceres.data.helper.CsvImportHelper;
import com.kingstar.ceres.data.impl.DefaultCsvDataInitFacade;
import com.kingstar.ceres.data.properties.FileParseProperties;
import com.kingstar.ceres.data.remote.ConnectionFactory;
import com.kingstar.ceres.data.strategy.CsvChannelManager;
import com.kingstar.ceres.data.strategy.csv.AbstractCsvChannel;
import com.kingstar.ceres.data.strategy.csv.ICsvChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@ConditionalOnBean({ITaskLockHandler.class, AbstractDataHandler.class})
@ConditionalOnResource(resources = {"classpath:fileMapping.xml"})
public class CsvImportAutoConfigure {

    @Resource
    private ITaskLockHandler taskLockHandler;

    @Resource
    private AbstractDataHandler dataHandler;

    @Resource
    private FileParseProperties fileParseProperties;


    @Bean
    public CsvImportHelper importHelper(CsvChannelManager csvChannelManager){
        return new CsvImportHelper(taskLockHandler,csvChannelManager);
    }

    @Bean
    public CsvChannelManager csvChannelManager(){
        return new CsvChannelManager(dataHandler,taskLockHandler,fileParseProperties);
    }

    @Bean(initMethod = "init")
    public DefaultCsvDataInitFacade csvDataInitFacade(@Autowired(required = false) ConnectionFactory connectionFactory){
        return new DefaultCsvDataInitFacade(taskLockHandler,connectionFactory);
    }

    @Bean(initMethod = "init")
    @ConditionalOnBean({DataSource.class})
    public AbstractCsvChannel defaultCsvChannel(@Autowired(required = false) DataSource dataSource, ITaskLockHandler taskLockHandler){
        return new OriginalCsvChannelImpl(dataSource,taskLockHandler);
    }
}
